﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Threading;
using Common.Library;

namespace Social_Celebrities.Views
{
    public partial class BloggerLogin : PhoneApplicationPage
    {
        string UserName = string.Empty;
        string Password = string.Empty;
        
        public BloggerLogin()
        {
            InitializeComponent();
            Loaded += BloggerLogin_Loaded;
        }

        void BloggerLogin_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("AddAccount.xaml"))
                NavigationService.RemoveBackEntry();
        }

        private void Signin_Click(object sender, RoutedEventArgs e)
        {
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += bg_DoWork;
            bg.RunWorkerCompleted += bg_RunWorkerCompleted;
            UserName=TbUser.Text;
            Password= TbPass.Text;
            bg.RunWorkerAsync();
           
           
        }

        void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Constants.CommentPosted == false)
            {
                MessageBoxResult result = MessageBox.Show("Authentication Error", "", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    NavigationService.GoBack();
                }
                else
                {
                    NavigationService.GoBack();
                }

            }
            else
            {
                Constants.CommentPosted = false;
                NavigationService.GoBack();
            }
        }

        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            PostToblog p = new PostToblog();
            p.Login(UserName, Password);
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            TbUser.Text = string.Empty;
            TbPass.Text = string.Empty;
            NavigationService.GoBack();
        }
    }
}